home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / qex / qexpad / pad.cpp < prev    next >
C/C++ Source or Header  |  1994-01-24  |  6KB  |  308 lines

  1. //pad.cpp - C++ program to calculate attenuator 
  2. //resistances and power dissipation
  3. //
  4. // N1BWT 1/94
  5. // compiled with Borland C++ version 3.1
  6.  
  7. #include <math.h>
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <iomanip.h>
  11. #include <ctype.h>
  12.  
  13. class Attenuator
  14. {
  15. public:
  16.     Attenuator();
  17.     float a()   { return ra; };       // end resistors
  18.     float b()   { return rb; };       // middle resistor
  19.     float c()   { return rc; };       // bridged-T resistors
  20.     float p1()  { return tp1; };      // power in first T resistor
  21.         float p2()  { return tp2; };      // power in second T resistor
  22.     float p3()  { return tp3; };      // power in third T resistor
  23.     float p4()  { return tp4; };      // power in bridge resistor
  24.  
  25.     void calc(float);            // calculations for T and pi
  26.     void bridge(float);             // calculations for bridged-T
  27.  
  28. private:
  29.         float ra;
  30.     float rb;
  31.         float rc;
  32.     float tp1;
  33.     float tp2;
  34.         float tp3;
  35.     float tp4;
  36. };
  37.  
  38. Attenuator::Attenuator()
  39. {       // Constructor
  40.     ra = 0;
  41.     rb = 999999;
  42.     rc = 0;
  43.     tp1 = 0;
  44.     tp2 = 0;
  45.     tp3 = 0;
  46.     tp4 = 0;
  47. }
  48.  
  49.  
  50. void Attenuator::calc(float loss)
  51. {       // calculations for T and pi attenuators
  52.     double tmp;
  53.  
  54.     tmp = pow(10,(loss/20));
  55.  
  56.     ra = (tmp - 1) / (tmp + 1);
  57.  
  58.     rb = 2 * tmp / ( pow(10,(loss/10)) - 1 ) ;
  59.  
  60. //    rc = tmp - 1;
  61.  
  62. // power: same for pi and T, so calculate for pi  (R = 1/rx)
  63. // assume one volt in, so P(R1) = V**2/R1 = 1/R1 = ra
  64. // since we know the power at the output
  65. // we know the voltage across the other two resistors
  66.  
  67.     double Vout;
  68.  
  69.     Vout = pow(10,(-loss/20));
  70.  
  71.     tp1 = 100 * ra;
  72.     tp3 = 100 * Vout * Vout * ra;
  73.     tp2 = 100 * (1 - Vout)*(1 - Vout) * rb;
  74.  
  75.  //   cout << "Vout = " << Vout << endl;
  76. }
  77.  
  78. void Attenuator::bridge(float loss)
  79. {
  80.     double tmp;
  81.  
  82.     tmp = pow(10,(loss/20));
  83.  
  84.     rc = tmp - 1;
  85.  
  86. // assume one volt in, Zo = 1;
  87.  
  88.     double Vout;
  89.     double iout, i1, i2, i3, i4;
  90.     
  91.     Vout = pow(10,(-loss/20));
  92.  
  93.     iout = Vout;        // only for readability, since r=1
  94.     
  95.     i4 = (1 - Vout) / rc;
  96.     
  97.     tp4 = 100 * i4 * i4 * rc;
  98.  
  99.     i3 = iout - i4;
  100.  
  101.     tp3 = 100 * i3 * i3;
  102.  
  103.     i1 = 1 - i4;
  104.  
  105.     tp1 = 100 * i1 * i1;
  106.  
  107.     tp2 = 100 * (i1 - i3) * (i1 - i3) / rc;      // r2 = 1/rc
  108. }
  109.  
  110.  
  111. // forward declarations
  112. void pad_out(float, float);
  113. void pad_header();
  114. void bridge_out(float, float);
  115. void bridge_header();
  116. void one_pad();
  117. void pad_table();
  118.  
  119. main()
  120. {
  121.     cout << " PAD.CPP - calculates resistance and power dissipation \n"
  122.      << "           for common attenuators. \n\n"
  123.      << " N1BWT 1994 \n\n";
  124.  
  125.     char mode = 'x';
  126.     
  127.     while ( mode != 'T' &&  mode != 'P')
  128.     {
  129.     cout << "\n"
  130.       << "Enter T for attenuator table, P for specific value: ";
  131.     cin >> mode;
  132.     mode = toupper(mode);
  133.     }
  134.  
  135.     switch(mode)
  136.     {
  137.     case 'P':
  138.         one_pad();
  139.         break;
  140.     case 'T':
  141.         pad_table();
  142.         break;
  143.     default:
  144.         cout  << "Huh?";
  145.     }
  146.  
  147.     return 0;
  148. }
  149.  
  150. void one_pad()
  151. {
  152.     float Zo;
  153.     float loss;
  154.  
  155.     cout << "\n" << "Enter characteristic impedance: ";
  156.     cin >> Zo;
  157.  
  158.     cout << "\n" << "Desired attenuation? ";
  159.     cin >> loss;
  160.  
  161.     pad_header();
  162.     
  163.     pad_out(loss, Zo);
  164.  
  165.     bridge_header();
  166.     
  167.     bridge_out(loss,Zo);
  168. }
  169.  
  170. void pad_table()
  171. {
  172.     float Zo;
  173.     float loss;
  174.     float initial;
  175.     float increment;
  176.     char type;
  177.  
  178.     cout << "\n" << "Enter characteristic impedance: ";
  179.     cin >> Zo;
  180.  
  181.     cout << "\n" << "Desired starting attenuation value? ";
  182.     cin >> initial;
  183.  
  184.     cout << "\n" << "Desired increment? ";
  185.     cin >> increment;
  186.  
  187.     cout << "\n" << "Configuration: Enter T for T and pi, B for Bridged-T: ";
  188.     cin >> type;  
  189.     type = toupper(type);    
  190.  
  191.     if (type == 'B')
  192.     {
  193.         bridge_header();
  194.  
  195.     for (int i = 0; i < 10; i++)
  196.         bridge_out((initial + i * increment), Zo);
  197.     }
  198.     else
  199.     {
  200.     pad_header();
  201.  
  202.     for (int i = 0; i < 15; i++)
  203.             pad_out((initial + i * increment), Zo);
  204.     }
  205. }
  206.     
  207. void pad_header()
  208. {
  209.     cout << "\n  T and pi attenuator \n\n";
  210.     cout << " Loss           T                    pi        "
  211.      << "  Power dissipation \n";
  212.     cout << "  dB     R1,R3       R2      R1,R3       R2     "
  213.      << " R1       R2       R3 \n";
  214.     cout << " ---     -----     -----     -----      -----   "
  215.          << "-----    -----    ----- \n";     
  216. }
  217.  
  218. void pad_out(float loss, float Zo)
  219. {    
  220.     Attenuator pad;
  221.  
  222.     pad.calc(loss);
  223.  
  224.  
  225.     cout << setprecision(1)
  226.      //     << "  "
  227.      << setw(4)
  228.      << loss
  229.      << "   "
  230.      << setw(7)
  231.      << setprecision(2)
  232.      << ( Zo * pad.a())
  233.      << "   "
  234.      << setw(7)
  235.      << ( Zo * pad.b())
  236.      << "   "
  237.      << setw(7)
  238.      << ( Zo /pad.a())
  239.      << "   "
  240.      << setw(7)
  241.      << ( Zo /pad.b())
  242.      << "   "
  243.      << setprecision(1)
  244.      << setw(5)
  245.      << pad.p1()
  246.      << "%   "
  247.      << setw(5)
  248.      << pad.p2()
  249.      << "%   "
  250.      << setw(5)
  251.      << pad.p3()
  252.      << "%   "
  253.      << "\n";
  254. }
  255.  
  256. void bridge_header()
  257. {
  258.     cout << " \n";
  259.     cout << " Bridged-T Attenuator       ------R4-----       \n";
  260.     cout << "                           |             |      \n";
  261.     cout << "   R1 = R3 = Zo           -+--R1--+--R3--+--    \n";
  262.     cout << "                                  |            \n";
  263.     cout << "                                  R2           \n";
  264.     cout << "                                  |            \n";
  265.     cout << "                                  V            \n\n";
  266.     cout << " Loss      Resistance     "
  267.      << "       Power dissipation \n";
  268.     cout << "  dB      R2         R4      "
  269.      << " R1       R2       R3       R4\n";
  270.     cout << " ---     -----     -----     "
  271.          << "-----    -----    -----    -----\n"; 
  272. }    
  273.  
  274. void bridge_out(float loss, float Zo)
  275. {    
  276.     Attenuator pad;
  277.  
  278.     pad.bridge(loss);
  279.  
  280.  
  281.     cout << setprecision(1)
  282.      //     << "  "
  283.      << setw(4)
  284.      << loss
  285.      << "   "
  286.      << setw(7)
  287.      << setprecision(2)
  288.      << ( Zo * pad.c())
  289.      << "   "
  290.      << setw(7)
  291.      << ( Zo /pad.c())
  292.      << "   "
  293.      << setprecision(1)
  294.      << setw(5)
  295.      << pad.p1()
  296.      << "%   "
  297.      << setw(5)
  298.      << pad.p2()
  299.      << "%   "
  300.      << setw(5)
  301.      << pad.p3()
  302.      << "%   "
  303.      << setw(5)
  304.      << pad.p4()
  305.      << "%   "
  306.      << "\n";
  307. }
  308.